import express, { json, text } from "express"
import path from "path"
import fs from "fs"
const publicUrl = path.join(__dirname,"./public")
const app = express()
app.listen(9999,()=>{
    console.log("服务器已开启");
})
app.use(
    //静态资源访问
    express.static(publicUrl)
)
app.get("/add",(req,res,text)=>{
    let day = new Date().toLocaleString()
    console.log(day);
    let urls = path.join(__dirname,"./public/bill.json")
    let arr = JSON.parse(fs.readFileSync(urls),"utf-8")
    let {obj} = req.query
    obj = JSON.parse(obj)
    console.log(req.query);
    obj.id = arr.length+""
    obj.data = day
  
    arr.push(obj)
    fs.writeFileSync(urls,JSON.stringify(arr),"utf-8")

    console.log(arr);
})
app.get("/gain",(req,res,text)=>{
    let {id = null} = req.query
    let urls = path.join(__dirname,"./public/bill.json")
    let arr = JSON.parse(fs.readFileSync(urls),"utf-8")
    if(id!== null){
        arr = arr.filter(item=>{return item.id === id})
    }
    res.send(arr)
})
app.get("/and",(req,res,text)=>{
    let urls = path.join(__dirname,"./public/user.json")
    let arr = JSON.parse(fs.readFileSync(urls),"utf-8")
    let {obj = null} = req.query
    if (obj !== null) {
        arr = []
        arr.push(JSON.parse(obj))
        fs.writeFileSync(urls,JSON.stringify(arr),"utf-8")
    }
    res.send(arr)
    
    // if(text !== null){
        
    // }else{
 
    //     // let {obj} = req.query
    //     // console.log(req.query);
    //     // obj.id = arr.length+""
    //     // obj.day = day
    //     // arr.push(JSON.parse(obj))
    //     // fs.writeFileSync(urls,JSON.stringify(arr),"utf-8")
    
       
    // }
    
})


